<?php
/**
 * Autor:   Miguel A. Zilli
 * Email:   miguelzilli@gmail.com
 */

class Revision extends AppModel {
    var $name = 'Revision';
    var $useTable = 'revisiones';
    var $primaryKey = 'id';
    var $displayField = 'tipo_rev';
    var $recursive = 0;
    var $actsAs = array('Containable');
    var $order=array(
        'Revision.fch_revision'=>'DESC',
        'Revision.tipo_rev'=>'ASC'
    );

    var $validate = array(
        'fch_revision'=>array(
            array('rule'=>'notEmpty','message'=>'Debe completar este dato.','required'=>true)
        ),
        'fch_relevamiento'=>array(
            array('rule'=>'notEmpty','message'=>'Debe completar este dato.','required'=>true)
        ),
        'tipo_rev'=>array(
            array('rule'=>'notEmpty','message'=>'Debe completar este dato.','required'=>true)
        ),
        'has_dedicadas_rev'=>array(
            array('rule'=>'numeric','message'=>'Solo se permiten números.','allowEmpty' => true)
        ),
        'sup_total'=>array(
            array('rule'=>'numeric','message'=>'Solo se permiten números.','allowEmpty' => true),
        )
    );

    var $belongsTo = array(
        'Productor' => array(
            'className' => 'Productor',
            'foreignKey' => 'productor_id'
        ),
        'Predio' => array(
            'className' => 'Predio',
            'foreignKey' => 'predio_id'
        ),
        'Actividadtipo' => array(
            'className' => 'Actividadtipo',
            'foreignKey' => 'actividadtipo_id'
        ),
        'Tecnico' => array(
            'className' => 'Tecnico',
            'foreignKey' => 'tecnico_id'
        )
    );

    var $hasMany = array(
        'CanalesRevision' => array(
            'className' => 'CanalesRevision',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Comercializacion' => array(
            'className' => 'Comercializacion',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Controlessanitario' => array(
            'className' => 'Controlessanitario',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Empleado' => array(
            'className' => 'Empleado',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Financiamiento' => array(
            'className' => 'Financiamiento',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Informestecnico' => array(
            'className' => 'Informestecnico',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Instalacion' => array(
            'className' => 'Instalacion',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Otrasactividad' => array(
            'className' => 'Otrasactividad',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Pcultural' => array(
            'className' => 'Pcultural',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Produccion' => array(
            'className' => 'Produccion',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Recocultivo' => array(
            'className' => 'Recocultivo',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Recoinstalacion' => array(
            'className' => 'Recoinstalacion',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Situacionesactual' => array(
            'className' => 'Situacionesactual',
            'foreignKey' => 'revision_id',
            'dependent' => false
        ),
        'Subsidio' => array(
            'className' => 'Subsidio',
            'foreignKey' => 'revision_id',
            'dependent' => false
        )
    );

    public function beforeSave($options = array()){
        $this->data['Revision']['fch_revision'] = $this->formatDate($this->data['Revision']['fch_revision']);
        if(empty($this->data['Revision']['fch_revision'])){
            $this->data['Revision']['fch_revision'] = date('Y-m-d H:i');
        }
        $this->data['Revision']['fch_relevamiento'] = $this->formatDate($this->data['Revision']['fch_relevamiento']);
        if(empty($this->data['Revision']['fch_relevamiento'])){
            $this->data['Revision']['fch_relevamiento'] = date('Y-m-d H:i');
        }
        if(!empty($this->data['Revision'])){
            foreach($this->data['Revision'] as $campo=>$valor){
                $this->data['Revision'][$campo]=strtoupper($valor);
            }
        }

        return parent::beforeSave($options);
    }

    public function afterFind($result) {
        foreach ($result as $clave => $valor) {
            if (isset($valor['Revision']['fch_revision'])) {
                $result[$clave]['Revision']['fch_revision'] = $this->formatoFechatAfterFind($valor['Revision']['fch_revision']);
            }
            if (isset($valor['Revision']['fch_relevamiento'])) {
                $result[$clave]['Revision']['fch_relevamiento'] = $this->formatoFechatAfterFind($valor['Revision']['fch_relevamiento']);
            }
            if (isset($valor['Revision']['created'])) {
                $result[$clave]['Revision']['created'] = $this->formatoFechatAfterFind($valor['Revision']['created']);
            }
        }
        return parent::afterFind($result);
    }

    function formatoFechatAfterFind($fecha) {
        return date('d-m-Y', strtotime($fecha));
    }

    function formatDate($dateToFormat){
        $pattern1 = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/i';
        $pattern2 = '/^([0-9]{4})\/([0-9]{2})\/([0-9]{2})$/i';
        $pattern3 = '/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/i';
        $pattern4 = '/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/i';

        $coincidences = array();

        if(preg_match($pattern1, $dateToFormat)){
            $newDate = $dateToFormat;
        }elseif(preg_match($pattern2, $dateToFormat, $coincidences)){
            $newDate = $coincidences[1] . '-' . $coincidences[2] . '-' . $coincidences[3];
        }elseif(preg_match($pattern3, $dateToFormat, $coincidences)){
            $newDate = $coincidences[3] . '-' . $coincidences[2] . '-' . $coincidences[1];
        }elseif(preg_match($pattern4, $dateToFormat, $coincidences)){
            $newDate = $coincidences[3] . '-' . $coincidences[2] . '-' . $coincidences[1];
        }else{
            $newDate = null;
        }
        return $newDate;
    }
}
?>